package com.zzty.daoimpl;

import com.zzty.common.DBUtil;
import com.zzty.common.DBUtilPool;
import com.zzty.dao.SelfExamTransactionDao;
import com.zzty.entity.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SelfExamTransactionDaoImpl implements SelfExamTransactionDao {
//    private static final Logger logger = LoggerFactory.getLogger(SelfExamDaoImpl.class);

//    public Integer addSelfTransaction(int userId) {
//        Date d=new Date();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String date=sdf.format(d);
//        String date2=sdf2.format(d);
//        System.out.println("现在时间为："+date);
//        Question question=new Question();
//        try {
//            PreparedStatement preparedStatement =new DBUtil().getPreparedStatement(
//                    "select * from selfExam where date=?");
//            preparedStatement.setString(1,date);
//            ResultSet resultSet =preparedStatement.executeQuery();
//            while(resultSet.next()){
//                question.setId(resultSet.getInt("questionId"));
//                int selfExamId=resultSet.getInt("id");
//                PreparedStatement pst=new DBUtil().getPreparedStatement(
//                        "insert into selfExamTransaction (selfExamId,userId,startTime) values(?,?,?)");
//                pst.setInt(1,selfExamId);
//                pst.setInt(2,userId);
//                pst.setString(3,date2);
//                pst.executeUpdate();
//                System.out.println("在selfExamTransaction插入数据成功；"
//                        +"selfExamId    "+selfExamId+"  userId  "+userId+"  date2   "+date2+"   questionId is "+question.getId());
//
//
//            }
////            resultSet.close();
////            DBUtil.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return question.getId();
//    }

//    public Boolean checkLogIn(int id) {
//        Boolean checkLogIn=false;
//        try {
//            PreparedStatement preparedStatement=DBUtil.getPreparedStatement(
//                    "select * from selfExamTransaction where userId=? and startTime is null");
//            preparedStatement.setInt(1,id);
//            ResultSet resultSet=preparedStatement.executeQuery();
//            while (resultSet.next()){
//                checkLogIn=true;
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return checkLogIn;
//    }


//    public void commit(Integer userId,String answerUrl) {
//        try {
//            Date nowDate=new Date();
//            PreparedStatement preparedStatement=DBUtil.getPreparedStatement(
//                    "select startTime from selfExamTransaction where userId=? and answerUrl is null ;");
//            preparedStatement.setInt(1,userId);
//            ResultSet resultSet=preparedStatement.executeQuery();
//            while(resultSet.next()){
//                Date startTime=resultSet.getTime("startTime");
//                logger.info("startTime:{}",startTime);
////                long minutes=(date2.getTime()-date1.getTime())/(1000*60);
//                long spendTime=(nowDate.getTime()-startTime.getTime())/(1000*60);
//                logger.info("spendTime:{}",spendTime);
//                PreparedStatement pst=DBUtil.getPreparedStatement(
//                        "update selfExamTransaction set answerUrl=?,spendTime=? where userId=? and answerUrl is null;");
//                pst.setString(1,answerUrl);
//                pst.setString(2,spendTime+"minutes");
//                pst.setInt(3,userId);
//                pst.executeUpdate();
//
//            }



//        } catch (Exception e) {
//            logger.error("SelfExamDaoImpl.commit error");
//            e.printStackTrace();
//        }
// }


//    public List searchMyQuestion() {
//        List<Question> questions=null;
//        try {
//            PreparedStatement pst=DBUtil.getPreparedStatement("select * from selfQuestion");
//            ResultSet rs=pst.executeQuery();
//            while(rs.next()){
//                if(questions==null){
//                    questions=new ArrayList<>();
//                }
//                Question question=new Question();
//                question.setId(rs.getInt("id"));
//                question.setContent(rs.getString("content"));
//                questions.add(question);
//            }
////            rs.close();
////           DBUtil.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return questions==null?null:questions;
//    }
//
//    @Override
//    public void searchMyAnswer() {
//
//    }
//
//    @Override
//    public void studentsWork() {
//
//    }

    public List getSelfExamToTable() {
        List <SelfExam> selfExams=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst = cn.prepareStatement("select * from selfExam");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                if(selfExams==null){
                    selfExams=new ArrayList<SelfExam>();
                }
                SelfExam selfExam=new SelfExam();
                selfExam.setId(rs.getInt("id"));
                selfExam.setClassId(rs.getInt("classId"));
                selfExam.setDate(rs.getDate("date"));
                selfExam.setQuestionId(rs.getString("questionId"));
                selfExams.add(selfExam);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return selfExams;
    }


//    public List searchSelfQuestion(){
//        List<SelfQuestion> selfQuestions=null;
//        try {
//            PreparedStatement preparedStatement=DBUtil.getPreparedStatement("select * from selfExam");
//            ResultSet resultSet=preparedStatement.executeQuery();
//            List<SelfExam> selfExams=null;
//            while(resultSet.next()){
//                if(selfExams==null){
//                    selfExams=new ArrayList<>();
//                }
//                SelfExam selfExam=new SelfExam();
//                selfExam.setQuestionId(resultSet.getString("questionId"));
//                selfExam.setDate(resultSet.getDate("date"));
//                selfExams.add(selfExam);
//            }
//
//            for(int i=0;i<selfExams.size();i++){
//                String questionId=selfExams.get(i).getQuestionId();
//                Date date=selfExams.get(i).getDate();
//                PreparedStatement pst=DBUtil.getPreparedStatement("select content from selfQuestion where id=?");
//                pst.setString(1,questionId);
//                ResultSet rs=pst.executeQuery();
//
//                while(rs.next()){
//                    if(selfQuestions==null){
//                        selfQuestions=new ArrayList<>();
//                    }
//                    SelfQuestion selfQuestion=new SelfQuestion();
//                    selfQuestion.setQuestion(rs.getString("content"));
//                    selfQuestion.setDate(date);
//                    selfQuestions.add(selfQuestion);
//                }
//            }
//
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//
//        return selfQuestions;
//    }


    public String searchAnswerUrlById(int examId,int userId) {
        SelfExamTransaction selfExamTransaction=new SelfExamTransaction();
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst = cn.prepareStatement(
                    "select answerUrl from selfExamTransaction where selfExamId=? and userId=?");
            pst.setInt(1,examId);
            pst.setInt(2,userId);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                selfExamTransaction.setAnswerUrl(rs.getString("answerUrl"));
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return selfExamTransaction.getAnswerUrl();
    }

//    @Override
//    public List getContentByDate(Date date ) {
//        List<SelfStudentHomework> studentHomeworkLIst=new ArrayList<>();
//        try {
//            PreparedStatement preparedStatement =new DBUtil().getPreparedStatement(
//                    "select * from selfExamTransaction where date_format(startTime,'%Y-%m-%d')=?;");
//            preparedStatement.setDate(1, (java.sql.Date) date);
//            ResultSet resultSet=preparedStatement.executeQuery();
//            while(resultSet.next()){
//                SelfStudentHomework selfStudentHomework=new SelfStudentHomework();
//                Integer userId=resultSet.getInt("userId");
//                Integer score=resultSet.getInt("score");
//                String answerUrl=resultSet.getString("answerUrl");
//                Date spendTime=resultSet.getDate("spendTime");
//                String userName=getUserNameById(userId);
//                selfStudentHomework.setName(userName);
//                selfStudentHomework.setScore(score);
//                selfStudentHomework.setAnswerUrl(answerUrl);
//                selfStudentHomework.setSpendTime(spendTime);
//                studentHomeworkLIst.add(selfStudentHomework);
//
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//        return studentHomeworkLIst;
//    }
//
//

//    @Override
//    public String getQuestionById(int id) {
//        Question question =new Question();
//        try {
//            PreparedStatement preparedStatement=DBUtil.getPreparedStatement(
//                    "select content from selfQuestion where id=?");
//            preparedStatement.setInt(1,id);
//            ResultSet resultSet=preparedStatement.executeQuery();
//            while(resultSet.next()){
//                question.setContent(resultSet.getString("content"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return question.getContent();
//    }

//    public String getUserNameById(Integer userId){
//        String userName="";
//        try {
//            PreparedStatement preparedStatement =new DBUtil().getPreparedStatement("select name from user where id=?");
//            preparedStatement.setInt(1,userId);
//            ResultSet resultSet=preparedStatement.executeQuery();
//            while(resultSet.next()){
//                userName=resultSet.getString("name");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return userName;
//    }
    public List createTable(){
        List<SelfExamTransaction> sets=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst = cn.prepareStatement("select * from selfExamTransaction");
            ResultSet rs = pst.executeQuery();
            while(rs.next()){
                if(sets==null) {
                    sets = new ArrayList<SelfExamTransaction>();
                }
                SelfExamTransaction selfExamTransaction=new SelfExamTransaction();
                selfExamTransaction.setUserId(rs.getInt("userId"));
                selfExamTransaction.setScore(rs.getInt("score"));
                selfExamTransaction.setStartTime(rs.getString("startTime"));
                selfExamTransaction.setSpendTime(rs.getString("spendTime"));
                sets.add(selfExamTransaction);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sets;

    }
    public String getNameById(Integer id) {
        User user = new User();
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst = cn.prepareStatement(
                    "select name from user where id=?");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                user.setName(rs.getString("name"));
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user.getName();
    }
}
